package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.estatewar.EstateBuildingInfo;
import com.yanqu.road.entity.estatewar.EstateFloorInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EstateWarDaoImpl extends TempDao {

	public void getEstateFloorInfoMap(Map<Integer, Map<Integer, EstateFloorInfo>> buildingFloorMap,
									  Map<Integer, EstateFloorInfo> floorIndexMap) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_estate order by id";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				EstateFloorInfo data = new EstateFloorInfo();
				data.setType(rs.getInt("type"));
				data.setPos(rs.getInt("pos"));
				data.setFloorIndex(rs.getInt("totalRanking"));
				data.setEarnSpeedAddition(rs.getInt("moneySpeedAdd"));
				data.setDailyReward(PropertyHelper.parseStringToProperty(rs.getString("dailyWine")));
				data.setFloorReward(PropertyHelper.parseStringToProperty(rs.getString("floorReward")));
				data.setInvitation(rs.getBoolean("invitation"));
				String npcAbilityList = rs.getString("monsterAbilityList");
				List<BigInteger> abilityList = new ArrayList<>();
				if(!StringUtils.isNullOrEmpty(npcAbilityList)){
					String[] arrayStr = npcAbilityList.split("\\|");
					for(String str : arrayStr){
						abilityList.add(new BigInteger(str));
					}
				}
				data.setNpcAbilityList(abilityList);
				if(!buildingFloorMap.containsKey(data.getType())){
					buildingFloorMap.put(data.getType(), new ConcurrentHashMap<>());
				}
				buildingFloorMap.get(data.getType()).put(data.getPos(), data);
				floorIndexMap.put(data.getFloorIndex(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
	}

	public Map<Integer, EstateBuildingInfo> getEstateBuildingInfoMap() {
		Map<Integer, EstateBuildingInfo> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_estate_building order by id";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				EstateBuildingInfo data = new EstateBuildingInfo();
				data.setId(rs.getInt("id"));
				data.setType(rs.getInt("type"));
				data.setNpc(rs.getBoolean("isRobot"));
				data.setCanWorship(rs.getBoolean("isCanWorship"));
				data.setName(rs.getString("name"));
				data.setSceneSystemList(StringUtils.stringToIntegerList(rs.getString("sceneSystem"), "\\|"));
				dataMap.put(data.getType(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
